<template>
  <div class="innerContainer">
    <common-title>
      <template #expand>
        <el-button @click="add()">新增</el-button>
        <el-button type="primary" @click="openImport(1)">导入</el-button>
      </template>
    </common-title>

    <div class="innerPane">
      <div class="filterArea">
        <el-form :inline="true" :model="filterForm" class="demo-form-inline">
          <el-form-item label="角色名称">
            <el-input @keyup.enter="search" v-model="searchForm.roleName" placeholder="请输入角色名称"></el-input>
          </el-form-item>
          <el-form-item label="角色编码">
            <el-input @keyup.enter="search" v-model="searchForm.roleName" placeholder="请输入角色编码"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button plain type="primary" @click="search">查询</el-button>
            <el-button plain @click="onClear">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <el-table :data="tableData" border row-key="id" style="width: 100%; " row-class-name="fixed-row"
        :cell-style="{ textAlign: 'center' }">
        <el-table-column header-align="center" prop="isSys" label="角色类型" width="200">
          <template #default="scope">
            <div class="name-wrapper" title="系统默认角色无法删除" v-if="scope.row.isSys === 1">
              <el-tag size="medium" type="warning">系统角色</el-tag>
            </div>
            <div class="name-wrapper" v-else-if="scope.row.isSys === 0">
              <el-tag size="medium">自定义角色</el-tag>
            </div>
          </template>
        </el-table-column>

        <el-table-column header-align="center" prop="roleCode" label="角色编码" width="180" />
        <el-table-column header-align="center" prop="roleName" label="角色名称" width="200" />

        <el-table-column header-align="center" prop="remark" label="备注" />
        <el-table-column label="操作" header-align="center" width="300" prop="col9">
          <template #default="scope">
            <el-link plain type="primary" size="small" @click="handleEdit(scope.row)">编辑</el-link> 
            <el-link plain v-if="scope.row.isSys === 0" type="danger" size="small"  @click="del(scope.row.id)">删除</el-link>

            <el-link plain type="success" size="small" @click="handleUser(scope.row)">关联用户</el-link>
            <el-link plain type="info" size="small" @click="handleMenuScope(scope.row)">菜单权限</el-link>

          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination style="margin-top: 20px" :current-page="searchForm.current" :page-size="searchForm.size"
        :page-sizes="[10, 20, 30, 40]" layout="->,total, sizes, prev, pager, next, jumper" :total="total"
        @size-change="handleSizeChange" @current-change="handleCurrentChange" />

    </div>

    <detail-view ref="viewObj"></detail-view>

    <select-menu ref="menuObj" @confirm="search"></select-menu>

    <select-user ref="userObj" @confirm="search"></select-user>
  </div>
</template>

<script setup>
import CommonTitle from "@/components/CommonTitle.vue";
import { onMounted, reactive, ref } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";

import DetailView from "@/views/role/view.vue";
import SelectMenu from "./selectMenu.vue";
import selectUser from "./selectUser.vue";

import { reqRoleList } from "../../utils/requests";
import { tr } from "element-plus/es/locales.mjs";

const router = useRouter();

// Dom 挂载之后
onMounted(() => {
  search();
});
// 用户数据
let tableData = ref([]);

let total = ref(0);
// 搜索条件
const searchForm = reactive({
  current: 1,
  size: 10,
  formName: "",
});

const onClear = () => {
  (searchForm.formName = ""), search();
};

const handleSizeChange = (size) => {
  searchForm.size = size;
  search();
};
const handleCurrentChange = (current) => {
  searchForm.current = current;
  search();
};

const search = () => {
  searchForm.current = 1;

  reqRoleList(searchForm).then((res) => {
    tableData.value = res.records;

    total.value = res.total;
  });
};

// 详情查看弹窗
const viewObj = ref();
const handleEdit = (row) => {
  let rowData = Object.assign({ _o: "v" }, row);
  viewObj.value.openDialog(rowData);
};


// 删除用户
const del = (id) => {
  ElMessageBox.confirm("确定要删除该角色吗?", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(async () => {
      const res = await userApi.delUser({ id: id });
      if (res.data.success) {
        ElMessage.success("删除成功");
        getUserList();
      } else {
        ElMessage.error("删除失败");
      }
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "取消删除",
      });
    });
};

const menuObj = ref();
const handleMenuScope = (row) => {
  menuObj.value.openDialog(row);
};

const userObj = ref();
const handleUser = (row) => {
  userObj.value.openDialog(row);
};

</script>

<style scoped>
.filterArea {
  display: flex;
  justify-content: space-between;
  padding-top: 10px;
}

.filterArea .el-select {
  width: 120px;
}

.el-table .el-link {
  margin-right: 10px;
}
</style>
